import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import index from '@/components/index'
import eventType from '@/components/eventType/index'
import eventTypeAdd from '@/components/eventType/add'
import rememberBigThing from '@/components/remember/bigThing'
import rememberDiary from '@/components/remember/diary'
import rememberBigThingList from '@/components/remember/bigThingList'
import rememberDiaryList from '@/components/remember/diaryList'
import other from '@/components/other'
import typeList from '@/components/charge/type-list'
import chargeStoreAdd from '@/components/charge/store-add'
import storeList from '@/components/charge/store-list'
import chargeAdd from '@/components/charge/add'
import chargeList from '@/components/charge/list'
import imageDetail from '@/components/remember/image-detail'
import login from '@/components/login'
import task from '@/components/task/index'

Vue.use(Router)

let router = new Router({
  routes: [
    {
      path: '/',
      name: 'index',
      component: index
    },
    {
      path: '/login',
      name: 'login',
      component: login
    },
    {
      path: '/test',
      name: 'test',
      component: HelloWorld
    },
    {
      path: '/eventType/index',
      name: 'eventType',
      component: eventType
    },
    {
      path: '/eventType/add',
      name: 'eventTypeAdd',
      component: eventTypeAdd
    },
    {
      path: '/remember/bigThingList',
      name: 'rememberBigThingList',
      component: rememberBigThingList
    },
    {
      path: '/remember/bigThing',
      name: 'rememberBigThing',
      component: rememberBigThing
    },
    {
      path: '/remember/diaryList',
      name: 'rememberDiaryList',
      component: rememberDiaryList
    },
    {
      path: '/remember/diary',
      name: 'rememberDiary',
      component: rememberDiary
    },
    {
      path: '/task',
      name: 'task',
      component: task
    },
    {
      path: '/other',
      name: 'other',
      component: other
    },
    {
      path: '/charge/type-list',
      name: 'typeList',
      component: typeList
    },
    {
      path: '/charge/store/add',
      name: 'chargeStoreAdd',
      component: chargeStoreAdd
    },
    {
      path: '/charge/store-list',
      name: 'storeList',
      component: storeList
    },
    {
      path: '/charge/add',
      name: 'chargeAdd',
      component: chargeAdd
    },
    {
      path: '/charge/list',
      name: 'chargeList',
      component: chargeList
    },
    {
      path: '/image/detail',
      name: 'imageDetail',
      component: imageDetail
    },
  ]
})

router.beforeEach((to, from, next) => {
  if(to.name !== 'login'){
    if(localStorage.getItem('token') === 'undefined' || !localStorage.getItem('token')){
      next('/login')
    }else{
      next()
    }
  }else{
    next()
  }
})

export default router
